//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2007
// (c) Rafal Fitt (fitt@aplusc.com.pl), 2004
//
// 'Options' dialog.
//

# include "StdAfx.h"
# include "Resource.h"
# include "Options.h"
# include "Settings.h"
# include "MapView.h"
# include "ResMan.h"
# include "Types.h"
# include "Globals.h"

void ShowOptions (size_t _cFirstPage) {
	// Prepare pages.
	const size_t c_cPages = 6;
	PROPSHEETPAGE pshp [c_cPages];
	::memset (pshp, 0, sizeof (pshp));
	for (size_t cPage = 0; cPage < c_cPages; ++ cPage) {
		pshp [cPage].dwSize    = sizeof (PROPSHEETPAGE);
		pshp [cPage].dwFlags   = PSP_DEFAULT;
		pshp [cPage].hInstance = g_hResInst;
	}

	pshp [0].pszTemplate = MAKEINTRESOURCE (IDD_OPTIONS_EDIT);
	pshp [0].pfnDlgProc  = _OptionsEditProc;

	pshp [1].pszTemplate = MAKEINTRESOURCE (IDD_OPTIONS_VIEW);
	pshp [1].pfnDlgProc  = _OptionsViewProc;

	pshp [2].pszTemplate = MAKEINTRESOURCE (IDD_OPTIONS_FILE);
	pshp [2].pfnDlgProc  = _OptionsFileProc;

	pshp [3].pszTemplate = MAKEINTRESOURCE (IDD_OPTIONS_UNITS);
	pshp [3].pfnDlgProc  = _OptionsUnitsProc;

	pshp [4].pszTemplate = MAKEINTRESOURCE (IDD_OPTIONS_WEBMAP);
	pshp [4].pfnDlgProc  = _OptionsWebMapProc;

	pshp [5].pszTemplate = MAKEINTRESOURCE (IDD_OPTIONS_COMMON);
	pshp [5].pfnDlgProc  = _OptionsCommonProc;

	// Prepare header.
	PROPSHEETHEADER psh;
	::memset (& psh, 0, sizeof (psh));
	psh.dwSize     = sizeof (psh);
	psh.dwFlags    = PSH_PROPSHEETPAGE;
	psh.hwndParent = g_hWnd;
	psh.hInstance  = g_hResInst;
	psh.nPages     = c_cPages;
	psh.nStartPage = _cFirstPage;
	psh.ppsp       = pshp;
	psh.pszCaption = "Options";

	::PropertySheet (& psh);
}

////////////////////////////////////////

static
INT_PTR CALLBACK _OptionsCommonProc (HWND _hWnd, UINT _uMsg, WPARAM _wParam, LPARAM _lParam) {
	switch (_uMsg) {
		case WM_INITDIALOG: {
			// Language.
			const HWND hLang = ::GetDlgItem (_hWnd, IDC_LANGUAGE);
			for (size_t l = 0; l < GetLanguagesNum (); ++ l) {
				::SendMessage (hLang, CB_INSERTSTRING, l, reinterpret_cast<LPARAM> (GetLanguageName (l).c_str ()));
				::SendMessage (hLang, CB_SETITEMDATA,  l, l);
			
				if (l == GetCurrentLanguage ())
					::SendMessage (hLang, CB_SETCURSEL, l, 0);
			}
		
			return TRUE;
		}

		case WM_COMMAND:
			switch (LOWORD (_wParam)) {
				case IDC_LANGUAGE:
					if (HIWORD (_wParam) == CBN_SELCHANGE) {
						PropSheet_Changed (::GetParent (_hWnd), _hWnd);
						return TRUE;
					}
					break;
			}
			break;

		case WM_NOTIFY: {
			const NMHDR * const pInfo = reinterpret_cast<const NMHDR *> (_lParam);
			switch (pInfo->code) {
				case PSN_APPLY: {
					// Language.
					const HWND hLang = ::GetDlgItem (_hWnd, IDC_LANGUAGE);
					const size_t cSelIndex = ::SendMessage (hLang, CB_GETCURSEL, 0, 0);
					if (cSelIndex == -1)
						break;
					const size_t l = ::SendMessage (hLang, CB_GETITEMDATA, cSelIndex, 0);

					ActivateLanguage (l);

					break;
				}
			}
		}
	}

	return FALSE;
}
